package applets;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.StringTokenizer;

/**
 * Created by IntelliJ IDEA.
 * User: I
 * Date: 07.12.2010
 * Time: 14:43:27
 * To change this template use File | Settings | File Templates.
 */
public class Applets extends Applet {
    public Control control = new Control();
    public LayoutManagers layoutManagers = new LayoutManagers();

    public void init() {
        //g.drawString("Hello World!", 20, 20);
        int width = 200;
        int height = 100;

        setLayout(new GridLayout(2, 1));
        add(control);
        add(layoutManagers);
    }

    class Control extends Panel {
        public Control() {
            setBackground(Color.ORANGE);
            setLayout(new GridLayout(3, 1));

            CheckboxGroup g = new CheckboxGroup();
            add(new Checkbox("FlowLayout", g, false)).addMouseListener(new EventsFlowLayout());
            add(new Checkbox("BorderLayout", g, false)).addMouseListener(new EventsBorderLayout());
            add(new Checkbox("GridLayout", g, false)).addMouseListener(new EventsGridLayout());
        }
    }

    public static class LayoutManagers extends Panel {
        public LayoutManagers() {
            setBackground(Color.GRAY);
            //addBorderLayout();
            //addFlowLayout();
            add(new Button("1"));
            add(new Button("2"));
            add(new Button("3"));
            add(new Button("4"));
        }

        public void addFlowLayout() {
            setLayout(new FlowLayout(FlowLayout.LEFT, 20, 6));
            removeAll();
            add(new Button("1"));
            add(new Button("2"));
            add(new Button("3"));
            add(new Button("4"));
        }

        public void addBorderLayout() {
            removeAll();
            setLayout(new BorderLayout());
            add("North", new Button("1"));
            add("South", new Button("2"));
            add("East", new Button("3"));
            add("West", new Button("4"));
        }

        public void addGridLayout() {
            removeAll();
            setLayout(new GridLayout(2, 2));
            add(new Button("1"));
            add(new Button("2"));
            add(new Button("3"));
            add(new Button("4"));
        }
    }

    class EventsFlowLayout implements MouseListener {

        public void mouseClicked(MouseEvent e) {
            layoutManagers.addFlowLayout();
            layoutManagers.setBackground(Color.BLACK);
            layoutManagers.repaint();
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }
    }

    class EventsBorderLayout implements MouseListener {

        public void mouseClicked(MouseEvent e) {
            layoutManagers.addBorderLayout();
            layoutManagers.setBackground(Color.BLACK);
            layoutManagers.repaint();
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }
    }

    class EventsGridLayout implements MouseListener {

        public void mouseClicked(MouseEvent e) {
            layoutManagers.addGridLayout();
            layoutManagers.setBackground(Color.BLACK);
            layoutManagers.repaint();
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }
    }
}
